// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

// 云函数入口函数
exports.main = async (event, context) => {
  let content = event.content
  if (content.length >= 20) {
    content = content.substr(0, 19) + '…'
  }
  try {
    const result = await cloud.openapi.subscribeMessage.send({
      touser: event.openid,
      page: 'pages/pyq/pyq',
      lang: 'zh_CN',
      data: {
        thing3: {
          value: content
        },
        time2: {
          value: event.time
        },
        thing1: {
          value: "有人给你发了评论，去看看吧~"
        }
      },
      templateId: 'TyKWm6BCbHIDLGItErh7k56f6IhDYNsM7z5uVQithI4',
      miniprogramState: 'formal'
    })
    return result
  } catch (err) {
    return err
  }
}